﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cqrs.Common;

namespace Cqrs.Common
{
    public class CommandEventGeneratorModel
    {
        public bool CreatesInitialState { private set; get; }
        public string EventName { get; private set; }
        public string DomainMethod { get; private set; }
        public IList<DomainProperty> AffectedProperties { get; private set; }
        public bool IsAdHocMethod { get; set; }

        public CommandEventGeneratorModel(string eventName,
            string domainMethod,
            IList<DomainProperty> affectedProperties,
            bool isAdHocMethod,
            bool createsInitialState)
        {
            this.EventName = eventName;
            this.DomainMethod = domainMethod;
            this.AffectedProperties = affectedProperties;
            this.IsAdHocMethod = isAdHocMethod;
            CreatesInitialState = createsInitialState;
        }

        public string GetParametersList(bool includeTypes, bool includeDomainName)
        {
            string _parameters = "";
            foreach (DomainProperty affectedProperty in this.AffectedProperties)
            {
                if (includeTypes)
                    _parameters += affectedProperty.Type + " " + FormatUtils.LowercaseFirst(affectedProperty.Name) + ", ";
                else if (includeDomainName)
                    _parameters += FormatUtils.LowercaseFirst(DomainMethod) + "." + affectedProperty.Name + ", ";
                else
                    _parameters += FormatUtils.LowercaseFirst(affectedProperty.Name) + ", ";
            }
            if (_parameters.Any())
                _parameters = _parameters.Remove(_parameters.Length - 2);
            return _parameters;
        }

        public string GetParameterValuesList()
        {
            string _parameters = "";
            foreach (DomainProperty affectedProperty in this.AffectedProperties)
            {
                switch (affectedProperty.Type.ToLower())
                {
                    case "string":
                        _parameters += GetRandomData() + ", ";
                        break;
                    case "datetime":
                        _parameters += @"DateTime.Now, ";
                        break;
                    default:
                        _parameters += "1, ";
                        break;
                }
            }
            if (_parameters.Any())
                _parameters = _parameters.Remove(_parameters.Length - 2);
            return _parameters;
        }

        public string GetRandomData()
        {
            string returnVal = "";
            foreach (DomainProperty property in this.AffectedProperties)
            {
                switch (property.Type.ToLower())
                {
                    case "string":
                        returnVal += @"""" + GenerateRandomString(_random.Next(12) + 1) + @""", ";
                        break;
                }
            }
            if (returnVal.Any())
                returnVal = returnVal.Remove(returnVal.Length - 2);
            return returnVal;
        }

        // http://stackoverflow.com/questions/1122483/c-sharp-random-string-generator
        private static readonly Random _random = new Random();
        private const string _letters = "abcdefghijklmnopqrstuvwxyz";
        private string GenerateRandomString(int maxNumberOfLetters)
        {
            char[] buffer = new char[maxNumberOfLetters];

            for (int i = 0; i < maxNumberOfLetters; i++)
            {
                buffer[i] = _letters[_random.Next(_letters.Length)];
            }
            return new string(buffer);
        }

        public static List<CommandEventGeneratorModel> BuildCommandEventGeneratorModelList(
            StateMachine stateMachine, List<CommandEvent> adHocMethods)
        {
            List<CommandEventGeneratorModel> commandEventGeneratorModels = new List<CommandEventGeneratorModel>();
            foreach (var state in stateMachine)
            {
                foreach (var transitionKeyValue in state.Value.Transitions)
                {
                    CommandEventGeneratorModel transition =
                        new CommandEventGeneratorModel(
                            transitionKeyValue.Value.Event,
                            transitionKeyValue.Value.Command,
                            transitionKeyValue.Value.AffectedProperties,
                            false,
                            stateMachine.InitialState == state.Key
                        );

                    if (commandEventGeneratorModels.Exists(p => p.EventName == transition.EventName))
                    {
                        CommandEventGeneratorModel eventModel =
                            commandEventGeneratorModels.Find(p => p.EventName == transition.EventName);
                        foreach (DomainProperty affectedProperty in transition.AffectedProperties)
                        {
                            if (eventModel.AffectedProperties.Count(p => p.Name == affectedProperty.Name) == 0)
                                eventModel.AffectedProperties.Add(affectedProperty);
                        }
                    }
                    else
                        commandEventGeneratorModels.Add(transition);
                }
            }

            foreach (CommandEvent commandEvent in adHocMethods)
            {
                CommandEventGeneratorModel ceGeneratorModel = new CommandEventGeneratorModel(
                    commandEvent.Event, commandEvent.Command, commandEvent.AffectedProperties, true, false);
                commandEventGeneratorModels.Add(ceGeneratorModel);
            }
            return commandEventGeneratorModels;
        }

    }
}
